$(function () {
  var form = layui.form;

  var q = {
    catename: '',
    page: 1,
    limit: 10,
  };

  // 渲染
  function getCateAll() {
    axios({
      method: 'GET',
      url: 'http://124.223.14.236:8060/admin/ydd_cate/list',
      headers: {
        'x-token': localStorage.getItem('x-token'),
      },
    }).then(function (res) {
      console.log(res.data);
      const htmlStr = template('cateTpl', res.data.data);
      $('tbody').html(htmlStr);
      getPaging(res.data.data.count);
    });
  }
  getCateAll();

  // 搜索按钮
  $('#sou').on('submit', function (e) {
    e.preventDefault();
    var catename = $('[name=tagname]').val();
    q.catename = catename;
    axios({
      method: 'GET',
      url: 'http://124.223.14.236:8060/admin/ydd_cate/list',
      params: { catename: catename },
      headers: {
        'x-token': localStorage.getItem('x-token'),
      },
    }).then(function (res) {
      console.log(res.data);
      const htmlStr = template('cateTpl', res.data.data);
      $('tbody').html(htmlStr);
      getPaging(res.data.data.count);
    });
  });

  // 重置按钮
  $('#resetBtn').on('click', function () {
    q.catename = '';
    getCateAll();
  });

  var addBtnClose = null;
  $('#addBtn').on('click', function () {
    addBtnClose = layer.open({
      title: '添加分类',
      type: 1,
      area: ['800px', '350px'],
      content: $('#addTpl').html(), //这里content是一个普通的String
    });
  });

  // 添加
  $('body').on('submit', '#addForm', function (e) {
    e.preventDefault();
    axios({
      method: 'POST',
      url: 'http://124.223.14.236:8060/admin/ydd_cate/add',

      data: $(this).serialize(),

      headers: {
        'x-token': localStorage.getItem('x-token'),
      },
    }).then(function (res) {
      layer.msg('添加成功');
      getCateAll();
      layer.close(addBtnClose);
    });
  });

  $('body').on('click', '#cancelBtn', function () {
    layer.close(addBtnClose);
  });

  // 删除
  $('tbody').on('click', '#delBtn', function () {
    var id = $(this).attr('data-id');
    layer.confirm('确认删除吗?', { icon: 3, title: '提示' }, function (index) {
      axios({
        method: 'GET',
        url: 'http://124.223.14.236:8060/admin/ydd_cate/del',
        params: {
          id: id,
        },
        headers: {
          'x-token': localStorage.getItem('x-token'),
        },
      }).then(function (res) {
        console.log(res);
        if (res.status !== 200) {
          return layer.msg('删除失败！');
        }
        layer.msg('删除成功！');
        getCateAll();
      });
      layer.close(index);
    });
  });

  // 修改
  $('tbody').on('click', '#writeBtn', function () {
    addBtnClose = layer.open({
      title: '添加分类',
      type: 1,
      area: ['800px', '350px'],
      content: $('#addTplDate').html(),
    });
    var id = {
      id: $(this).attr('data-id'),
    };

    axios({
      method: 'GET',
      url: 'http://124.223.14.236:8060/admin/ydd_cate/rowInfo',
      params: {
        id: id.id,
      },
      headers: {
        'x-token': localStorage.getItem('x-token'),
      },
    }).then(function (res) {
      console.log('id=========' + JSON.stringify(id));
      console.log('res.data======' + JSON.stringify(res));
      form.val('addFormDate', res.data.data);
    });
  });

  $('body').on('submit', '#addFormDate', function (e) {
    e.preventDefault();
    axios({
      method: 'POST',
      url: 'http://124.223.14.236:8060/admin/ydd_cate/update',

      data: $(this).serialize(),

      headers: {
        'x-token': localStorage.getItem('x-token'),
      },
    }).then(function (res) {
      layer.msg('修改成功');
      getCateAll();
      layer.close(addBtnClose);
    });
  });

  // 分页区域
  function getPaging(count) {
    layui.use('laypage', function () {
      var laypage = layui.laypage;
      //执行一个laypage实例
      laypage.render({
        elem: 'test1', //注意，这里的 test1 是 ID，不用加 # 号
        count: count, //数据总数，从服务端得到
        limit: q.limit,
        curr: q.page,
        limits: [2, 3, 4, 5],
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
          q.page = obj.curr;
          q.limit = obj.limit;
          if (!first) {
            axios({
              method: 'GET',
              url: 'http://124.223.14.236:8060/admin/ydd_cate/list',
              params: { catename: '', page: obj.curr, limit: obj.limit },
              headers: {
                'x-token': localStorage.getItem('x-token'),
              },
            }).then(function (res) {
              console.log(res.data);
              const htmlStr = template('cateTpl', res.data.data);
              $('tbody').html(htmlStr);
              getPaging(res.data.data.count);
            });
          }
        },
      });
    });
  }

  $('#vanishTop').on('click', function () {
    $('#sou').hide();
    $(this).hide();
    $('#vanishDown').show();
  });
  $('#vanishDown').on('click', function () {
    $('#sou').show();
    $(this).hide();
    $('#vanishTop').show();
  });

  let b = true;
  $('.layui-icon-app').on('click', function () {
    if (b) {
      $('.adv-box').show();
      b = false;
    } else {
      $('.adv-box').hide();
      b = true;
    }
  });

  $('#export').click(function (e) {
    var table = $('.layui-table');
    var preserveColors = table.hasClass('table2excel_with_colors') ? true : false;
    $(table).table2excel({
      exclude: '.noExl',
      name: 'Excel Document Name',
      filename: 'myFileName' + new Date().toISOString().replace(/[\-\:\.]/g, '') + '.xls',
      fileext: '.xls',
      exclude_img: true,
      exclude_links: true,
      exclude_inputs: true,
      preserveColors: preserveColors,
    });
  });
});
